package com.mjk.manager.magicapi.core.core.service.impl;


import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.magicapi.core.core.model.ApiInfo;
import com.mjk.manager.magicapi.core.core.service.AbstractPathMagicResourceStorage;
import com.mjk.manager.magicapi.core.utils.PathUtils;

import java.util.Objects;

public class ApiInfoMagicResourceStorage extends AbstractPathMagicResourceStorage<ApiInfo> {

    private final String prefix;

    public ApiInfoMagicResourceStorage(String prefix) {
        this.prefix = ObjectUtils.isEmpty(prefix) ? "" : prefix + "/";
    }

    @Override
    public String folder() {
        return "api";
    }

    @Override
    public Class<ApiInfo> magicClass() {
        return ApiInfo.class;
    }

    @Override
    public String buildMappingKey(ApiInfo info, String path) {
        return info.getMethod().toUpperCase() + ":" + super.buildMappingKey(info, path);
    }

    @Override
    public String buildMappingKey(ApiInfo info) {
        return PathUtils.replaceSlash(buildMappingKey(info, this.prefix + Objects.toString(magicResourceService.getGroupPath(info.getGroupId()), "")));
    }

    @Override
    public void validate(ApiInfo entity) {
        notBlank(entity.getMethod(), REQUEST_METHOD_REQUIRED);
        super.validate(entity);
    }
}
